﻿namespace Revit同步插件.MEP建模
{

  using Autodesk.Revit.DB;
  using Autodesk.Revit.UI;
using GrevitAPI.Apps;
  using System.Linq;

  [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
  class 疯狂计算 : IExternalCommand
  {


    UIApplication _uiapp;
    public Autodesk.Revit.UI.Result Execute(Autodesk.Revit.UI.ExternalCommandData commandData,
    ref string message,
    Autodesk.Revit.DB.ElementSet elements)
    {

      try
      {
        _uiapp = commandData.Application;
        UIDocument uidoc = _uiapp.ActiveUIDocument;
        Document doc = uidoc.Document;

        var rf = uidoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element);
        var instance = doc.GetElement(rf) as FamilyInstance;
        LogManager.LogFile = @"D:\日志\Mep\mm.txt";

        using (Transaction t = new Transaction(doc, "疯狂改参数"))
        {
          t.Start();
          var mP = instance.LookupParameter("公称半径");
          var sP = instance.LookupParameter("支管公称半径");
          for(int i = 1;i < 10000; i++)
          {
            sP.Set(i / 304.8);
            var carray = (instance.MEPModel.ConnectorManager.Connectors.Cast<Connector>().ToArray() );
            var str =carray[0].Origin.ToString();
            str += carray[1].Origin.ToString();
            LogManager.Write(str + "\r\n");
          }
          t.RollBack();
        }



        return Autodesk.Revit.UI.Result.Succeeded;
      }
      catch (System.Exception ex)
      {
        message = ex.Message;
        return Autodesk.Revit.UI.Result.Failed;
      }
    }
  }

}
